<?php
/**
 * Created by JetBrains PhpStorm.
 * User: vstukanov
 * Date: 8/19/11
 * Time: 12:09 PM
 * To change this template use File | Settings | File Templates.
 */
 
class ZendX_Db_Table extends Zend_Db_Table {

    /**
     * @param array | int $ids
     * @return mixed
     */
    public function getItems($ids) {
        $cache = Zend_Registry::get('Zend_Cache');
        if(is_array($ids)) {
            $fail_ids = array();
            $from_cache = array();
            $result = NULL;
            foreach($ids as $id) {
                if(FALSE === ($item = $cache->load("applicationDb{$this->_name}item$id"))) {
                    $fail_ids[] = (int) $id;
                } else {
                    $from_cache[] = $item;
                }
            }
            if(!empty($fail_ids)) {
                foreach(($result = $this->fetchAll('id IN ('.implode(',',$fail_ids).')')) as $item) {
                    $cache->save($item->toArray(), "applicationDb{$this->_name}item{$item->id}");
                }
            }
            $from_db = array();
            if($result !== NULL) {
                $from_db = $result->toArray();
            }
            return new Zend_Db_Table_Rowset(array('data' => array_merge_recursive($from_cache, $from_db), 'table' => $this));
        } else {
            $id = (int) $ids;
            if(FALSE === ($data = $cache->load("applicationDb{$this->_name}item$id"))) {
                $item = $this->fetchRow("id = $id");
                $cache->save($item->toArray(), "applicationDb{$this->_name}item$id");
                return $item;
            }
            return new Zend_Db_Table_Row(array('data' => $data, 'table' => $this));
        }
    }
}